package com.renyr.controller;

import com.renyr.model.FilePathProperties;
import com.renyr.model.ResponseData;
import com.renyr.service.ImgService;
import com.renyr.utils.Common;
import com.renyr.utils.Constant;
import com.renyr.utils.StateCode;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.swagger.annotations.Api;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ZeroCopyHttpOutputMessage;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Mono;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

@RestController
@RequestMapping("/img")
@Api(value="/test", tags="图片服务接口")
public class ImgController {

	@Autowired
	private ImgService service;

	@Autowired
	private FilePathProperties properties;

	/**
	 * 展示图片
	 * @param response
	 * @param fileName
	 */
	@GetMapping(value="/{fileName}")
	@Cacheable(cacheNames = "showImgByName")
	public Mono<Void> showImgByName(ServerHttpResponse response, @PathVariable(name="fileName") String fileName) {
		HttpResponse response1 = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
		ZeroCopyHttpOutputMessage zeroCopyResponse = (ZeroCopyHttpOutputMessage) response;
//		response.getHeaders().set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename="+fileName);
		response.getHeaders().set("Access-Control-Allow-Origin", "*");
		response.getHeaders().setContentType(MediaType.IMAGE_PNG);
		File file = new File(properties.getFilePath() + fileName);
		return zeroCopyResponse.writeWith(file, 0, file.length());
	}
	
	/**
	 * 保存图片
	 * @param file
	 * @return
	 */
	@PostMapping(value="/saveImg")
	public ResponseData saveImg(@RequestParam(value="file") MultipartFile file){
		if (null == file || file.isEmpty()) {
			return new ResponseData();
		}
		if (file.getSize() > Constant.MAX_IMG_SIZE) {
			ResponseData data = new ResponseData();
			data.setMsg("上传文件过大");
			return data;
		}
		if (!Constant.IMG_TYPE.contains(file.getContentType())) {
			ResponseData data = new ResponseData();
			data.setMsg("文件格式有误");
			return data;
		}
		String saveFileName = service.saveImg(file);
		return new ResponseData<>(saveFileName);
	}
	
	/**
	 * 批量保存图片
	 * @param files
	 * @return
	 */
	@PostMapping(value="/saveImgs")
	public ResponseData saveImgs(@RequestParam(value="file") MultipartFile[] files){
		for (MultipartFile file : files) {
			if (file.isEmpty()) {
				return new ResponseData();
			}
			if (file.getSize() > Constant.MAX_IMG_SIZE) {
				ResponseData data = new ResponseData();
				data.setMsg("上传文件过大");
				return data;
			}
			if (!Constant.IMG_TYPE.contains(file.getContentType())) {
				ResponseData data = new ResponseData();
				data.setMsg("文件格式有误");
				return data;
			}
		}
		String saveFileName = service.saveImgs(files);
		return new ResponseData<>(saveFileName);
	}
	
	/**
	 * 删除图片
	 * @param filename
	 * @return
	 */
	@DeleteMapping(value="/deleteImgByName")
	public ResponseData deleteImgByName(@RequestParam(value="filename")String filename) {
		ResponseData data = new ResponseData();
		boolean deleteImg = service.deleteImg(filename);
		data.setData(filename);
		if (deleteImg) {
			data.setCode(StateCode.SUCCESS.getCode());
			data.setMsg("删除成功");
			return data;
		}
		data.setMsg("删除失败");
		return data;
	}
	
	/**
	 * 批量删除图片
	 * @param filenames
	 * @return
	 */
	@DeleteMapping(value="/deleteImgsByName")
	public ResponseData deleteImgsByName(@RequestParam(value="filenames")String[] filenames) {
		ResponseData data = new ResponseData();
		if (null != filenames) {
			boolean deleteImg = service.deleteImgs(filenames);
			data.setData(filenames);
			if (deleteImg) {
				data.setCode(StateCode.SUCCESS.getCode());
				data.setMsg("删除成功");
				return data;
			}
		}
		data.setMsg("删除失败");
		return data;
	}
}
